﻿using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using System;
using System.Linq;
using System.Windows;
using PDFDesign.Properties;
using CefSharp;
using CefSharp.Wpf;

namespace PDFDesign
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            InitPalette();
            InitCef();
        }

        /// <summary>
        /// 初始化Cef
        /// </summary>
        private void InitCef()
        {
            try
            {
                var cefSettings = new CefSettings
                {
                    Locale = "zh-CN",
                    AcceptLanguageList = "zh-CN,zh;q=0.8",
                    PersistUserPreferences = true,
                };
                Cef.Initialize(cefSettings);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 初始化配色方案
        /// </summary>
        private void InitPalette()
        {
            try
            {
                var paletteHelper = new PaletteHelper();
                var accentColor = new SwatchesProvider().Swatches.FirstOrDefault(s => s.Name == Settings.Default.AccentColor);
                if (accentColor != null) paletteHelper.ReplaceAccentColor(accentColor);
                var primaryColor = new SwatchesProvider().Swatches.FirstOrDefault(s => s.Name == Settings.Default.PrimaryColor);
                if (primaryColor != null) paletteHelper.ReplacePrimaryColor(primaryColor);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
